<?php

namespace app\api\controller\admin\setting;

use app\api\common\controller\BaseApi;
use app\api\services\admin\setting\DeptServices;
use app\Request;
use think\App;

class DeptController extends BaseApi
{

    /**
     * @var DeptServices
     */
    private \app\api\services\admin\setting\DeptServices $services;

    public function __construct(Request $request, App $app, DeptServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 显示资源列表
     *
     * @param \app\Request $request
     *
     * @return \think\response\Json
     * @throws \ReflectionException
     */
    public function select(Request $request): \think\response\Json
    {
        $where = $request->getParams([
            ['name', '', '', 'name_like'],
            ['delete_time', null],
            ['status', 1],
        ]);

        $this->validate($where, [
            'name_like' => 'max:32',
        ], [], true, true);
        $ret = $this->services->select($where);
        return ret_value(0, "ok", $ret['list'], 200, [], ["count" => $ret['count']]);
    }

    /**
     * 保存新建资源
     *
     * @param \app\Request $request
     *
     * @return \think\response\Json
     */
    public function insert(Request $request): \think\response\Json
    {
        $data    = $request->getParams([
            ['pid', 0],
            ['department_code', ''],
            ['department_name', ''],
            ['contacts'],
            ['mobile_phone'],
            ['telephone'],
            ['email'],
            ['address'],
            ['status', 1],
        ]);
        $rule    = [
            'department_code' => 'require|length:2,32',
            'department_name' => 'require|length:2,32',
            'contacts'        => 'require|chsAlpha',
            'email'           => 'email',
            'mobile_phone'    => 'require|mobile',

        ];
        $message = [
            'department_name.require' => '部门名称不能为空',
            'department_code.require' => '部门代码不能为空',
            'contacts.require'        => '联系人不能为空',
            'mobile_phone.require'    => '手机号不能为空',
            'mobile_phone.mobile'     => '手机号格式错误',
            'email.email'             => '邮箱格式错误',
        ];
        $this->validate($data, $rule, $message);
        $this->services->save($data);
        return ret_value(0, "Success");
    }

    /**
     * 保存更新资源
     *
     * @param \app\Request $request
     *
     * @return \think\response\Json
     */
    public function update(Request $request): \think\response\Json
    {
        $where = $request->getParams([['id']]);
        $data  = $request->getParams([
            ['pid', 0],
            ['department_code'],
            ['department_name'],
            ['contacts'],
            ['mobile_phone'],
            ['telephone'],
            ['email'],
            ['address'],
            ['status', 1],
        ]);
        $this->validate($where, [
            'id' => 'require',
        ]);
        $rule    = [
            'department_code' => 'require|length:2,32',
            'department_name' => 'require|length:2,32',
            'contacts'        => 'require|chsAlpha',
            'email'           => 'email',
            'mobile_phone'    => 'require|mobile',

        ];
        $message = [
            'department_name.require' => '部门名称不能为空',
            'department_code.require' => '部门代码不能为空',
            'contacts.require'        => '联系人不能为空',
            'mobile_phone.require'    => '手机号不能为空',
            'mobile_phone.mobile'     => '手机号格式错误',
            'email.email'             => '邮箱格式错误',
        ];
        $this->validate($data, $rule, $message);

        $ret = $this->services->update($where, $data);
        if ($ret) {
            return ret_value(0, "Success");
        } else {
            return ret_value(-1, "Fail");
        }
    }

    /**
     * delete  删除指定资源
     *
     * @param \app\Request $request
     *
     * @return \think\response\Json
     */
    public function delete(Request $request): \think\response\Json
    {
        $data = $request->getParams([
            ['data'],
        ]);
        $this->validate($data, [
            'data' => 'require',
        ]);
        $ids = explode(',', $data['data']);
        $ret = $this->services->delete($ids);
        if ($ret)
            return ret_value(0, "Success");
        else
            return ret_value(-1, "Fail");
    }

    /**
     * 获取指定资源
     *
     * @param $id
     *
     * @return \think\response\Json
     */
    public function get($id): \think\response\Json
    {
        $where = ['id' => $id];
        $this->validate($where, [
            'id' => 'require',
        ]);
        $ret = $this->services->getOne($where, "id, pid, department_code, department_name, contacts, mobile_phone, telephone, email,address,status,create_time,update_time")->toArray();
        return ret_value(0, "", $ret);
    }

}
